package com.git.hui.yinshua.biz.rest.admin.order;

import com.git.hui.yinshua.api.model.ListRsp;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.order.PayStatusEnum;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.order.service.AppOrderService;
import com.git.hui.yinshua.biz.order.service.OrderService;
import com.git.hui.yinshua.biz.pay.service.PaymentService;
import com.git.hui.yinshua.biz.pay.service.RefundService;
import com.git.hui.yinshua.biz.rest.models.order.req.OrderQueryReq;
import com.git.hui.yinshua.biz.rest.models.order.rsp.OrderDto;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * @author YiHui
 * @date 2024/6/24
 */
@RestController
@Permission(role = UserAccessRole.ADMIN)
@RequestMapping(path = "admin/order")
public class AdminOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AppOrderService appOrderService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private RefundService refundService;

    /**
     * 订单列表查询
     *
     * @param req
     * @return
     */
    @RequestMapping(path = "list")
    public ResVo<ListRsp<OrderDto>> list(OrderQueryReq req) {
        req.autoInitReq();
        req.formatTime();
        if (StringUtils.isNotBlank(req.getStatus())) {
            req.setState(Splitter.on(",").splitToStream(req.getStatus()).map(Integer::valueOf).collect(Collectors.toList()));
        }
        if (StringUtils.isBlank(req.getNickName())) {
            req.setNickName(null);
        }
        if (StringUtils.isBlank(req.getReceiveName())) {
            req.setReceiveName(null);
        }
        if (StringUtils.isBlank(req.getGoodsName())) {
            req.setGoodsName(null);
        }
        if (StringUtils.isBlank(req.getReceivePhone())) {
            req.setReceivePhone(null);
        }

        ListRsp<OrderDto> list = orderService.listAndCount(req);
        return ResVo.ok(list);
    }

    /**
     * 发货
     *
     * @param orderId
     * @param trackingCompanyCode
     * @param trackingCode
     * @return
     */
    @GetMapping(path = "toReceive")
    public ResVo<Boolean> delivery(@RequestParam(name = "orderId") Long orderId, @RequestParam(name = "trackingCompanyCode") String trackingCompanyCode, @RequestParam(name = "trackingCode") String trackingCode) {
        return ResVo.ok(orderService.deliveryOrder(orderId, trackingCompanyCode, trackingCode));
    }

    /**
     * 下载用户上传的图，并将订单状态更新为待发货
     *
     * @param orderId
     * @return
     */
    @GetMapping(path = "toDelivery")
    public ResVo<Boolean> alreadyDownloadImgs(@RequestParam(name = "orderId") Long orderId) {
        return ResVo.ok(orderService.updateToDelivery(orderId));
    }

    /**
     * 下载用户上传的图片
     *
     * @param orderId
     * @return
     */
    @GetMapping(path = "download")
    public ResVo<Boolean> imgDownload(@RequestParam(name = "orderId") Long orderId) {
        return ResVo.ok(orderService.updateDownCnt(orderId));
    }

    /**
     * 用户后台设置为执行成功
     *
     * @param orderId
     * @return
     */
    @GetMapping(path = "pay")
    @Permission(role = UserAccessRole.SUPER_ADMIN)
    public ResVo<Boolean> pay(Long orderId) {
        return ResVo.ok(orderService.paymentCallback(orderId, PayStatusEnum.SUCCEED, System.currentTimeMillis()));
    }

    /**
     * 后台取消订单
     *
     * @param orderId
     * @return
     */
    @GetMapping(path = "cancel")
    @Permission(role = UserAccessRole.SUPER_ADMIN)
    public ResVo<Boolean> cancel(Long orderId) {
        return ResVo.ok(orderService.orderCancel(orderId));
    }


    /**
     * 后台做订单退款申请
     *
     * @param orderId 订单id
     * @param notes   提示信息
     * @return
     */
    @GetMapping(path = "refund")
    @Permission(role = UserAccessRole.SUPER_ADMIN)
    public ResVo<Boolean> refund(Long orderId, String notes) {
        return ResVo.ok(refundService.refunding(orderId, notes));
    }
}
